package mao.mashibing.juc.atest;

import mao.thinkinjava.concurency.countdownlatch.CountDownLatchDemo;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/*
user：mao
data：2020/11/22
note：使用两个CountDownLatch
*/
public class WithCountLatch {
    static CountDownLatch latch = new CountDownLatch(1);
    static CountDownLatch latch1 = new CountDownLatch(1);
    List<Object> list = new LinkedList<>();
    void add(Object o){list.add(o);}
    int size(){return list.size();}

    public static void main(String[] args) {
        WithCountLatch withCountLatch = new WithCountLatch();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                if (i == 5){
                    latch.countDown();
                    try {
                        latch1.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                withCountLatch.add(new Object());
                System.out.println("生产了第"+i+"个了");
            }
        }).start();
        new Thread(()->{
            try {
                latch.await();
                System.out.println("监测到五个了");
                latch1.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
